package com.example.zhinongbao.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.zhinongbao.Fragment.MyProductsFragment;
import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.ViewPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;

public class MyProductsActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_products);

        // 设置Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("我的货品");

        // 初始化Tab栏
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);

        setupViewPager();

        // 关联TabLayout和ViewPager2
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0: tab.setText("全部"); break;
                case 1: tab.setText("在售"); break;
                case 2: tab.setText("已下架"); break;
                case 3: tab.setText("审核中"); break;
            }
        }).attach();
    }

    private void setupViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new MyProductsFragment("all"));
        fragments.add(new MyProductsFragment("selling"));
        fragments.add(new MyProductsFragment("off_shelf"));
        fragments.add(new MyProductsFragment("reviewing"));

        ViewPagerAdapter adapter = new ViewPagerAdapter(this, fragments);
        viewPager.setAdapter(adapter);
    }
}